using UnityEngine;

public class ActorSFX : MonoBehaviour
{
    [Header("Audio Settings")]
    [SerializeField] private AudioSource audioSource;

    [Header("Spatial Audio")]
    [SerializeField] private bool use3DSound = true;
    [SerializeField] private float minDistance = 1f;
    [SerializeField] private float maxDistance = 15f;

    private void Awake()
    {
        if (audioSource == null)
        {
            audioSource = GetComponent<AudioSource>();
            if (audioSource == null)
            {
              
                return;
            }
        }

        ConfigureSpatialAudio();
        
    }

    private void ConfigureSpatialAudio()
    {
        if (audioSource == null || !use3DSound)
            return;

        audioSource.spatialBlend = 1f;
        audioSource.rolloffMode = AudioRolloffMode.Linear;
        audioSource.minDistance = minDistance;
        audioSource.maxDistance = maxDistance;
        audioSource.dopplerLevel = 0f;

    
    }

    public void PlaySFX(AudioClip clip)
    {
        if (audioSource == null)
        {
            Debug.LogError($"[ActorSFX] AudioSource null em {gameObject.name}");
            return;
        }

        if (clip == null)
        {
         
            return;
        }

        audioSource.PlayOneShot(clip);
       ;
    }

    public void PlayLoopingSFX(AudioClip clip)
    {
        if (audioSource == null)
        { 
            return;
        }

        if (clip == null)
        {

            return;
        }

        audioSource.clip = clip;
        audioSource.loop = true;
        audioSource.Play(); 
    }

    public void StopSFX()
    {
        if (audioSource != null)
        {
            audioSource.Stop();
        }
    }
}